package b_workqueue;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;
import org.junit.Test;
import utils.RabbitMQUtils;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 雷哥
 * @Date: 2020/10/06/22:54
 * @Description: 消费者1
 */
public class Consumer2 {

  @Test
  public void receiveMessage() throws  Exception{

    Connection connection = RabbitMQUtils.getConnection();
    //创建信道
    Channel channel = connection.createChannel();
    //声明队列
    channel.queueDeclare("hello",false,false,false,null);
    //接收消息
    channel.basicConsume("hello",true,new DefaultConsumer(channel){
      @Override
      public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties,
          byte[] body) throws IOException {
        System.out.println("消息者【2】接收到消息："+new String(body));
      }
    });
    System.in.read();
    RabbitMQUtils.closeChannelAndConnection(channel,connection);
  }

}
